06. Thymeleaf - Scope

035ND C01 L03 A10 THYMELEAF SCOPE

Instructions

In this example, we are going to reuse our existing code and show how to use these three scopes in Thymeleaf.

Add a method in our existing controller.

@RequestMapping("demo3")
public String demo3(HttpServletRequest request, Model model) {
   // Request
   request.setAttribute("request", "request data");
   // Session
   request.getSession().setAttribute("session", "session data");
   // Application
   request.getSession().getServletContext().setAttribute("application", "application data");
   return "demo2";
}

In this example, we are going to reuse our existing code and show how to use these three scopes in Thymeleaf.And add the following code in our demo2.html

<h3>Thymeleaf Scope</h3>
Request: <span th:text="${#httpServletRequest.getAttribute('request')}"></span><br />
Session: <span th:text="${session.session}"></span><br />
Application: <span th:text="${application.application}"></span><br />

Start application and goto http://localhost:8080/demo3

Demo

035ND C01 L03 A12 SCOPE EXAMPLE

Scope Quiz

When a bean has scope limited to only HTTP request, what is that called ?

SOLUTION: Request scope